<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.title" placeholder="文章标题" clearable></el-input>
      </el-form-item>
      <el-form-item prop="parentName">
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="id"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input v-model="dataForm.parentName" v-popover:menuListPopover
                  placeholder="点击选择文章菜单" @clear="clearInput()" clearable
                  class="menu-list__input"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('mh:content:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('mh:content:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        fixed="left"
        type="index"
        label="序号"
        width="80"
        align="center">
      </el-table-column>
      <el-table-column
        prop="categoryId"
        header-align="center"
        align="center"
        label="文章菜单">
        <template slot-scope="scope">
          <span>{{ scope.row.mhMenu.name }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        label="标题">
      </el-table-column>
      <el-table-column
        prop="author"
        header-align="center"
        align="center"
        label="作者">
      </el-table-column>
      <el-table-column
        prop="infoForm"
        header-align="center"
        align="center"
        label="信息来源">
      </el-table-column>
      <el-table-column
        prop="content"
        header-align="center"
        align="center"
        label="文章正文">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="contentShowClick(scope.row)">点击查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="url"
        header-align="center"
        align="center"
        label="外链接">
      </el-table-column>
      <el-table-column
        prop="type"
        header-align="center"
        align="center"
        label="文章类型">
        <template slot-scope="scope">
          <span v-if="scope.row.type===0">富文本</span>
          <span v-if="scope.row.type===1">外链接</span>
          <span v-if="scope.row.type===2">期刊</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="coverPath"
        header-align="center"
        align="center"
        label="期刊封面">
      </el-table-column>
      <el-table-column
        prop="filePath"
        header-align="center"
        align="center"
        label="附件">
      </el-table-column>
      <el-table-column
        prop="releaseTime"
        header-align="center"
        align="center"
        label="发布时间">
      </el-table-column>
      <el-table-column
        prop="canNum"
        header-align="center"
        align="center"
        label="浏览次数">
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        label="上次修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="120"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <el-dialog
      title="文章内容"
      :visible.sync="contentShow"
      width="70%">
      <div style="overflow-y: auto;height: 500px">
        <div style="text-align: center;">
          <h2 style="color:#BF1108">{{ contentInfo.title }}</h2>
          <div>
            <span>作者：{{ contentInfo.author }}</span>
            <span>发布日期：{{ contentInfo.releaseTime }}</span>
            <span>信息来源：{{ contentInfo.infoForm }}</span>
            <span>浏览次数{{ contentInfo.canNum }}次</span>
            <span>[关闭]</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div v-html="contentInfo.content"></div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="contentShow = false">取 消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import AddOrUpdate from './mhcontent-add-or-update'
import {treeDataTranslate} from '@/utils'

export default {
  data() {
    return {
      dataForm: {
        title: '',
        parentName: '',
        categoryId:''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      contentShow: false,
      contentInfo: '',
      menuList:[],
      menuListTreeProps: {
        label: 'name',
        children: 'children'
      },
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getParentMenuList()
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.dataForm.pageSize = this.pageSize
      this.dataForm.pageNumber = this.pageIndex
      this.$http({
        url: this.$http.adornUrl('/mh/content/page'),
        method: 'get',
        params: this.$http.adornParams(this.dataForm)
      }).then(({data}) => {
        if (data.list) {
          this.dataList = data.list
          this.totalPage = data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/mh/content/delete'),
          method: 'delete',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },

    contentShowClick(content) {
      this.contentShow = true
      this.contentInfo = content
    },

    //查询父级菜单
    getParentMenuList() {
      this.$http({
        url: this.$http.adornUrl(`/mh/menu/list`),
        method: 'get',
        params: this.$http.adornParams({'status': 0})
      }).then(({data}) => {
        if (data.success) {
          let dataList = data.data
          for (let i = 0; i < dataList.length; i++) {
            if (dataList[i].parentId && dataList[i].parentId > 0) {
              for (let j = 0; j < dataList.length; j++) {
                if (dataList[j].id === dataList[i].parentId) {
                  dataList[i].parentName = dataList[j].name;
                }
              }
            }
          }
          for (let j = 0; j < dataList.length; j++) {
            if (dataList[j].parentId === '0') {
              dataList[j].parentName = "一级菜单";
            }
          }
          this.menuList = treeDataTranslate(dataList, 'id')
        }
      })
    },
// 菜单树选中
    menuListTreeCurrentChangeHandle(data, node) {
      this.dataForm.categoryId = data.id
      this.dataForm.parentName = data.name
    },
    // 菜单树设置当前选中节点
    menuListTreeSetCurrentNode() {
      console.log(this.dataForm.categoryId)
      this.$refs.menuListTree.setCurrentKey(this.dataForm.categoryId)
      this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
      console.log(this.$refs.menuListTree)
    },

    clearInput() {
      this.dataForm.categoryId = undefined
      this.dataForm.parentName = undefined
    },
  }
}
</script>
